<?php
session_start();
include 'nav.inc.php';
html_head('Stundenübersicht');
include 'dbConn.php';
if(!isset($_SESSION["username"])){
	echo("</head>");
	echo("<body>");
	echo("<div class='content'>");
	echo ("Bitte erst <a href='../login.php'>einloggen</a>.");
	exit;
	echo("</div>");
	echo("</body>");
	echo("</html>");
}
else{
	echo("<link rel='stylesheet' type='text/css' href='tcal.css' />");
	echo("<script type='text/javascript' src='tcal.js'></script> ");
	echo("<script type='text/javascript' src='clone_this.js'></script> ");
	echo("</head>");
	echo("<body>");
	menu();
	echo("<div class='content'>");

	if (isset($_REQUEST['jahr']))
		$jahr = $_REQUEST['jahr'];
	else
		$jahr = date("Y");
	echo ("<form action='".$_SERVER['SCRIPT_NAME']."' method='post'>\n"); 
	echo ("<select name='jahr' size='1'>\n");

	for ($tmp = date("Y") ; $tmp<=date("Y") +2; $tmp++){
		if ($tmp == $jahr)
			echo ("<option selected='selected'>$tmp</option>\n");
		else
			echo ("<option>$tmp</option>\n");
	}

	echo ("</select>\n");
	echo ("<input type='submit' value='Ausw&auml;hlen'>\n");
	echo ("</form>\n");
	echo("<br/>");
	if (isset($_REQUEST['speichern'])){
 		for ($i=0; $i<sizeof($_POST['name']); $i++){
 			if(!empty($_POST['day'][$i]) && !empty($_POST['name'][$i])){
 				$date = explode("/", $_POST['day'][$i]);
 				if(intval($date[2]) > 2000){
 					$tmp = $date[2];
 					$date[2] = $date[1];
 					$date[1] = $date[0];
 					$date[0] = $tmp;
 				}
 				$date = implode("-", $date);
 				
 				$sql = "SELECT * FROM std_Feiertag WHERE name = '".$_POST['name'][$i]."' AND YEAR(datum) = '".$_POST['jahr']."'";
  				$result = mysql_query($sql) or die (mysql_error());
  				$row = mysql_fetch_assoc($result);
	
  				if (mysql_num_rows($result) == 0)
 					$sql = "INSERT INTO std_Feiertag VALUES ('', '$date', '".$_POST['name'][$i]."')";
 				else 
 					$sql = "UPDATE std_Feiertag SET datum = '$date' WHERE name = '".$_POST['name'][$i]."' AND YEAR(datum) = '".$_POST['jahr']."'";
				mysql_query($sql) or die (mysql_error());
 			}
			
 		}
		echo ("<div style='color:green;'>Erfolgreich eingetragen!</div>\n");
	}
	echo ("<form action='".$_SERVER['SCRIPT_NAME']."' method='post'>\n"); 
	echo ("<table>\n");	
	
		$feiertage = array('Neujahr', 'Karfreitag',
				'Ostermontag', 'Tag der Arbeit',
				'Christi Himmelfahrt','Pfingstmontag','Fronleichnam',
				'Mariä Himmelfahrt','Tag der deutschen Einheit','Allerheiligen',
				'1. Weihnachtstag','2. Weihnachtstag');
		 
		foreach ($feiertage as $feiertag){
			echo ("<tr>");
			echo ("<td>");
			echo $feiertag;
			echo("</td>");
			$sql = "SELECT * FROM std_Feiertag WHERE name = '$feiertag' AND YEAR(datum) = '$jahr'";
			$result = mysql_query($sql) or die (mysql_error());
			$row = mysql_fetch_assoc($result);
			
			echo("<td>");
			if (empty($row['datum'])){
				echo("<input type='text' name='day[]' class='tcal' value='' />");
				echo("<input type='hidden' name='name[]' value='".$feiertag."'>");
			}
			else{
				$date = explode("-", $row['datum']);
				$date = implode("/", $date);
				echo("<input type='text' name='day[]' class='tcal' value='".$date."' />");
				echo("<input type='hidden' name='name[]' value='".$feiertag."'>");
			}
			echo("</td>");
			echo("</tr>");
		}

	echo ("</table>\n");	
	echo ("<input type='submit' name='speichern' value='Speichern'>\n");
	echo ("<input type='hidden' name='jahr' value='$jahr'>\n");
	echo ("</form>\n");
	echo("</div>");
}
?>

</body>
</html>
